package cn.lili.modules.member.serviceimpl;

import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.SnowFlake;
import cn.lili.modules.member.entity.dos.RedPackInviteHelp;
import cn.lili.modules.member.entity.dos.RedPackTask;
import cn.lili.modules.member.mapper.RedPackInviteHelpMapper;
import cn.lili.modules.member.service.RedPackInviteHelpService;
import cn.lili.modules.member.service.RedPackUserService;
import cn.lili.modules.payment.entity.dto.UserRedPackUpdateDTO;
import cn.lili.modules.payment.entity.enums.UserRedPackServiceEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pojos.FyUser;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
@AllArgsConstructor
public class RedPackInviteHelpServiceImpl extends ServiceImpl<RedPackInviteHelpMapper, RedPackInviteHelp>
        implements RedPackInviteHelpService {

    private final RedPackUserService redPackUserService;

    /**
     * 添加邀新任务数据
     * @param fyUserList
     * @param redPackTask
     * @return 本次新增用户数
     */
    @Override
    public int addInviteHelp (List<FyUser> fyUserList, RedPackTask redPackTask) {
        int count = 0;
        if (!fyUserList.isEmpty()) {
            for (FyUser fyUser : fyUserList) {
                LambdaQueryWrapper<RedPackInviteHelp> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(RedPackInviteHelp::getTaskId, redPackTask.getId());
                queryWrapper.eq(RedPackInviteHelp::getUserId, UserContext.getCurrentId());
                queryWrapper.eq(RedPackInviteHelp::getHelpUserId, fyUser.getId());
                queryWrapper.last("limit 1");
                RedPackInviteHelp redPackInviteHelp = this.getOne(queryWrapper);
                if (null == redPackInviteHelp) {
                    // 保存上下级绑定关系
                    redPackInviteHelp = new RedPackInviteHelp();
                    redPackInviteHelp.setId(SnowFlake.getIdStr());
                    redPackInviteHelp.setHelpUserId(fyUser.getId());
                    redPackInviteHelp.setUserId(UserContext.getCurrentId());
                    redPackInviteHelp.setOpenId(Objects.requireNonNull(UserContext.getCurrentUser()).getOpenId());
                    redPackInviteHelp.setTaskId(redPackTask.getId());
                    redPackInviteHelp.setCreateTime(fyUser.getCreateTime());
                    redPackInviteHelp.setRedPackFlag(0);
                    this.save(redPackInviteHelp);
                    count++;
                }else {
                    if (redPackInviteHelp.getRedPackFlag() == 0) {
                        count++;
                    }
                }
            }
        }
        return count;
    }

    /**
     * 获取未领取红包用户
     * @param taskId
     * @return
     */
    @Override
    public List<RedPackInviteHelp> getListByFlag (String taskId) {
        LambdaQueryWrapper<RedPackInviteHelp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RedPackInviteHelp::getTaskId, taskId);
        queryWrapper.eq(RedPackInviteHelp::getUserId, UserContext.getCurrentId());
        queryWrapper.eq(RedPackInviteHelp::getRedPackFlag, 0);
        return this.list(queryWrapper);
    }

    /**
     * 更新用户红包领取状态
     * @param redPackInviteHelpList
     */
    @Override
    public void updateInviteHelp (List<RedPackInviteHelp> redPackInviteHelpList) {
        if (!redPackInviteHelpList.isEmpty()) {
            redPackInviteHelpList.forEach(redPackInviteHelp -> {
                redPackInviteHelp.setRedPackFlag(1);
                this.updateById(redPackInviteHelp);
            });
        }
    }
}
